package com.treeHole.treeHoleServer.controller;


import com.treeHole.treeHoleServer.pojo.RequestDataDTO.GetListGeneralDTO;
import com.treeHole.treeHoleServer.pojo.RequestDataDTO.PostDeletePostDTO;
import com.treeHole.treeHoleServer.pojo.RequestDataDTO.SearchPostDTO;
import com.treeHole.treeHoleServer.pojo.RequestDataDTO.UPCIdDTO;
import com.treeHole.treeHoleServer.pojo.Result;
import com.treeHole.treeHoleServer.pojo.ResultDataDTO.ResultPostDTO;
import com.treeHole.treeHoleServer.pojo.ResultDataDTO.ResultPostDetailDTO;
import com.treeHole.treeHoleServer.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class PostController {
    @Autowired
    private PostService postService;

    @RequestMapping("/like")
    public Result userLikePost(@RequestBody UPCIdDTO upcIdDTO) {
        int resultCode=postService.userLikePost(upcIdDTO.getUserId(), upcIdDTO.getPostId());
        log.info("收到用户点赞请求 upcIdDTO:{},处理结果:{}", upcIdDTO,resultCode);
        return resultCode==1?Result.success():Result.error("点赞失败!");
    }

    @RequestMapping("/unlike")
    public Result userUnlikePost(@RequestBody UPCIdDTO upcIdDTO) {
        int resultCode=postService.userUnlikePost(upcIdDTO.getUserId(),upcIdDTO.getPostId());
        log.info("收到用户取消点赞请求 upcIdDTO:{},处理结果:{}", upcIdDTO,resultCode);
        return resultCode==1?Result.success():Result.error("取消点赞失败!");
    }

    @RequestMapping("/favorite")
    public Result userFavoritePost(@RequestBody UPCIdDTO upcIdDTO) {
        int resultCode= postService.userCollectPost(upcIdDTO.getUserId(),upcIdDTO.getPostId());
        log.info("收到用户收藏请求 upcIdDTO:{},处理结果:{}", upcIdDTO,resultCode);
        return resultCode==1?Result.success():Result.error("收藏失败!");

    }
    @RequestMapping("/unfavorite")
    public Result userUnfavoritePost(@RequestBody UPCIdDTO upcIdDTO) {
        int resultCode =postService.userUnCollectPost(upcIdDTO.getUserId(),upcIdDTO.getPostId());
        log.info("收到用户取消收藏请求 upcIdDTO:{},处理结果:{}", upcIdDTO,resultCode);
        return resultCode==1?Result.success():Result.error("取消收藏失败!");
    }
    @RequestMapping("/new")
    public Result userNewPost(@RequestBody GetListGeneralDTO getListGeneralDTO) {
        List<ResultPostDTO> resultList=postService.getNewestPost(
                getListGeneralDTO.getTagId(),
                getListGeneralDTO.getUserId(),
                getListGeneralDTO.getPage());
        log.info("收到最新页请求,getListGeneralDTO:{}", getListGeneralDTO);
        return Result.success(resultList);
    }
    @RequestMapping("/hot")
    public Result userHotPost(@RequestBody GetListGeneralDTO getListGeneralDTO) {
        List<ResultPostDTO> resultList=postService.getHottestPost(
                getListGeneralDTO.getUserId()
        );
        log.info("收到最热页请求,getListGeneralDTO:{}", getListGeneralDTO);
        return Result.success(resultList);
    }
    @RequestMapping("/content")
    public Result userPostDetail(@RequestBody UPCIdDTO upcIdDTO) {
        ResultPostDetailDTO result=postService.getPostDetail(upcIdDTO.getPostId(),upcIdDTO.getUserId());
        log.info("收到获取帖子详细请求,upcIdDTO:{}", upcIdDTO);
        return Result.success(result);
    }
    @RequestMapping("/createPost")
    public Result userCreatePost(@RequestBody PostDeletePostDTO postDeletePostDTO){
        Integer postId= postService.userCreatePost(postDeletePostDTO);
        log.info("收到发布帖子请求,postDeletePostDTO:{}", postDeletePostDTO);
        return Result.success(postId);
    }
    @RequestMapping("/deletePost")
    public Result userDeletePost(@RequestBody PostDeletePostDTO postDeletePostDTO){
        int resultCode=postService.userDeletePost(postDeletePostDTO.getPostId());
        log.info("收到删除帖子请求,postDeletePostDTO:{}", postDeletePostDTO);
        return resultCode==1?Result.success():Result.error("删除帖子失败!");
    }
    @RequestMapping("/search")
    public Result userSearchPost(@RequestBody SearchPostDTO searchPostDTO) {
        List<ResultPostDTO> resultList=postService.searchPost(
                searchPostDTO.getQuery(),
                searchPostDTO.getUserId(),
                searchPostDTO.getTag());
        log.info("收到查询帖子请求,searchPostDTO:{}", searchPostDTO);
        return Result.success(resultList);
    }
}
